package cn.com.scooper.impl.xf;

import cn.com.scooper.dao.xf.MineIotDeviceDao;
import cn.com.scooper.pojo.po.MineIotDevicePo;
import cn.com.scooper.pojo.po.MineIotIndexPo;
import cn.com.scooper.service.xf.MineIotDeviceService;
import cn.com.scooper.service.xf.MineIotIndexService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class MineIotDeviceServiceImpl implements MineIotDeviceService {
    @Autowired
    private MineIotDeviceDao mineIotDeviceDao;
    @Autowired
    private MineIotIndexService mineIotIndexService;

    private static final Logger logger = LoggerFactory.getLogger(MineIotDeviceServiceImpl.class);

    @Override
    public MineIotDevicePo getMineIotDeviceByDeviceId(String deviceId) {
        return this.mineIotDeviceDao.selectByDeviceId(deviceId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveMineIotDevice(MineIotDevicePo mineIotDevicePo) {
        String equipName = mineIotDevicePo.getEquipName();
        logger.info("equipName"+equipName);
        int i = mineIotDeviceDao.insert(mineIotDevicePo);
        List<MineIotIndexPo> datas = mineIotDevicePo.getDatas();
        if (!CollectionUtils.isEmpty(datas)) {
            //当设备指标数据不为空时，新增指标数据
            mineIotIndexService.batchSaveMineIotIndex(datas);
        }
        return i;
    }

    @Override
    public int batchRemoveMineIotDevice(List<String> deviceIdList) {
        int i = mineIotDeviceDao.batchDeleteByDeviceIdList(deviceIdList);
        mineIotIndexService.batchRemoveMineIotIndex(deviceIdList);
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateMineIotDevice(MineIotDevicePo mineIotDevicePo) {
        String equipName = mineIotDevicePo.getEquipName();
        logger.info("equipName"+equipName);
        int i = mineIotDeviceDao.updateByPrimaryKey(mineIotDevicePo);
        //设备指标直接进行删除然后重新添加
        mineIotIndexService.removeMineIotIndexByDeviceId(mineIotDevicePo.getSourceId());
        List<MineIotIndexPo> datas = mineIotDevicePo.getDatas();
        if (!CollectionUtils.isEmpty(datas)) {
            //当设备指标数据不为空时，新增指标数据
            mineIotIndexService.batchSaveMineIotIndex(datas);
        }
        return i;
    }

    @Override
    public List<MineIotDevicePo> getMineIotDeviceList(MineIotDevicePo mineIotDevicePo) {
        List<MineIotDevicePo> list = mineIotDeviceDao.selectAll(mineIotDevicePo);
        return list;
    }
}